﻿Imports System.Data
Partial Class FctHierarchy_Add
    Inherits System.Web.UI.Page
    Public AlertText As String
    Public ScriptText As String
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Session("users") = "bb0011"
        Label1.Text = "ระบบจัดทำงบประมาณ > เพิ่มหน่วยงานใต้สังกัด"
        Dim adapter As New FundCenterParentTableAdapters.FundCentersTableAdapter
        Dim datatable As New FundCenterParent.FundCentersDataTable
        datatable = adapter.GetDataByCode(Request("FctCode"), Val(Request("PlanYear")))
        For Each row As FundCenterParent.FundCentersRow In datatable
            ParentLabel.Text = row.Description
        Next
        If Session("users") Is Nothing Then
            ScriptText = "<script> window.parent.TINY.box.hide();</script>"
        End If
    End Sub
    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adap As New FundCenterParentTableAdapters.FundCentersTableAdapter
        Dim dt As New FundCenterParent.FundCentersDataTable
        Dim planyear = Val(Request("PlanYear"))
        dt = adap.GetData(planyear)


        e.DataSource = dt

    End Sub
    Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim ds As New DataSet
        Dim dt As DataTable = ds.Tables.Add("Items")
        dt.Columns.Add("level", Type.GetType("System.String"))
        dt.Columns.Add("Description", Type.GetType("System.String"))
        Dim r As DataRow


        e.DataSource = dt
        r = dt.NewRow()
        r("level") = "10"
        r("Description") = "สาขา"
        dt.Rows.Add(r)
        r = dt.NewRow()
        r("level") = "11"
        r("Description") = "ส่วน"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("level") = "20"
        r("Description") = "เขต"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("level") = "30"
        r("Description") = "ภาค"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("level") = "31"
        r("Description") = "ฝ่าย"
        dt.Rows.Add(r)
        r = dt.NewRow()
        r("level") = "40"
        r("Description") = "สายงาน"
        dt.Rows.Add(r)

        r = dt.NewRow()
        r("level") = "50"
        r("Description") = "ฝบป."
        dt.Rows.Add(r)
    End Sub

   
    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click
        Dim FctCode As String
        Dim parent_orglevel As String
        Dim org_level As String
        Dim level As Integer
        Dim description As String
        Dim HierarchieID As Decimal
        FctCode = WebCombo1.Value
        org_level = WebCombo2.Value

        Dim adapter As New FundCenterParentTableAdapters.FundCenterHierarchiesTableAdapter
        Dim datatable As New FundCenterParent.FundCenterHierarchiesDataTable
        Dim exist As Integer
        exist = adapter.CheckExist(WebCombo1.Value, Request("Group"), Val(Request("PlanYear")))
        If exist > 0 Then
            Alert("ปรากฎหน่วยงานนี้ในโครงสร้างแล้ว ไม่สามารถบันทึกข้อมูลได้")
            Exit Sub
        End If

        datatable = adapter.GetDataByCode(Request("FctCode"), Request("Group"), Val(Request("PlanYear")))
       
        For Each row As FundCenterParent.FundCenterHierarchiesRow In datatable
            parent_orglevel = row.OrgLevel.ToString
            HierarchieID = row.Id
        Next
        If Val(Left(org_level, 1)) >= Val(Left(parent_orglevel, 1)) Then
            Alert("หน่วยงานลูกต้องมีระดับน้อยกว่าหน่วยงานแม่")
            Exit Sub
        End If
        If Val(Left(org_level, 1)) = 5 Then
            level = 1
        ElseIf Val(Left(org_level, 1)) = 4 Then
            level = 2
        ElseIf Val(Left(org_level, 1)) = 3 Then
            level = 3
        ElseIf Val(Left(org_level, 1)) = 2 Then
            level = 4
        ElseIf Val(Left(org_level, 1)) = 1 Then
            level = 5

        End If
        Dim f_adapter As New FundCenterParentTableAdapters.FundCentersTableAdapter
        Dim f_datatable As New FundCenterParent.FundCentersDataTable
        f_datatable = f_adapter.GetDataByCode(FctCode, Val(Request("PlanYear")))
        For Each f_row As FundCenterParent.FundCentersRow In f_datatable
            description = f_row.Description
        Next
        adapter.InsertQuery(Val(Request("PlanYear")), Request("Group"), level, org_level, description, Now(), Nothing, Session("users"), Nothing, FctCode, Val(Request("PlanYear")), HierarchieID, Request("FctCode"), Val(Request("PlanYear")))
        ScriptText = "<script> window.parent.TINY.box.hide();</script>"
    End Sub
    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub
End Class
